<?php
$this->breadcrumbs = array(
	'项目材料分析'
);
?>
<div
	id="project-material-analyze-container"
	style="width: 960px; height: 600px;"></div>

<?php 
$dataUrl = CJSON::encode($this->createUrl('projectMaterialAnalyzeData'));
$viewMaterialUrl = CJSON::encode($this->createUrl('viewProject',array(
	'MaterialStatisticForm'=>array(
		'projectId'=>'__projectId__',
		'materialModelId'=>'__materialModelId__'
		)
)));
$projects=Project::getAll();
$projectsJSON=CJSON::encode($projects);
$js=<<<EOF
Ext.onReady(function() {
    Ext.define('JX.Model.Material.ProjectAnalyze', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'id', type: 'string'},
            {name: 'name', type: 'string'},
            {name: 'unit', type: 'string'},
            {name: 'totalRequisition', type: 'float'},
            {name: 'totalWarehouse', type: 'float'},
            {name: 'totalReceipted', type: 'float'},
            {name: 'averagePrice', type: 'float'},
            {name: 'maxUnitPrice', type: 'float'},
            {name: 'averageUnitPrice', type: 'float'},
            {name: 'minUnitPrice', type: 'float'},
            {name: 'grandCost', type: 'float'}
        ]
    });

    var modelStore = Ext.create('Ext.data.TreeStore', {
        model: 'JX.Model.Material.ProjectAnalyze',
		root: {
	        text: '总览'
	    },
        proxy: {
            type: 'ajax',
            url: $dataUrl
        },
        folderSort: false
    });
	var projectCombo = Ext.create('Ext.form.field.ComboBox', {
        hideLabel: true,
        store: Ext.create('Ext.data.ArrayStore', {
            data: $projectsJSON,
            fields: [{
            	name: 'id',
            	mapping: 'id',
            	type: 'int'
			},{
				name: 'name',
				mapping: 'name',
				type:'string'
			}]
		}),
        displayField: 'name',
		valueField: 'id',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'选择一个项目.....',
        selectOnFocus:true,
        width:235
    });
	var keywordInput=Ext.create('Ext.form.field.Trigger',{
		triggerCls: 'x-form-clear-trigger',
        emptyText: '输入关键词过滤物品....',
        width:180,
		onTriggerClick: function() {
        	this.setRawValue('');
		}
	});
	var reloadFunction=function(){modelStore.load();};
	projectCombo.on('select',reloadFunction);
	keywordInput.on('specialkey',function(){
		if(e.getKey()==e.ENTER){
			reloadFunction();
		}  
	});
	var modelStoreBeforeload = function(store, operation, options) {
		Ext.apply(store.proxy.extraParams, {
			projectId : projectCombo.getValue(),
			keyword: keywordInput.getValue()
		});
	};
	var searchButton=Ext.create('Ext.Button', {
	    text: '查看',
	    handler: reloadFunction,
		iconCls: 'icon-magnifier'
	});
	modelStore.on('beforeload', modelStoreBeforeload);
	var nf=Ext.util.Format.numberRenderer('0,000.00');
	var renderFn=function(value){
		if(value!=0){
			return nf(value);
		}
		return '';
	};
	var summaryText=Ext.create('Ext.toolbar.TextItem',{
		text: '摘要'
	});
	modelStore.on('load', function(store,node,records){
		if(node.get('id')=='root'){
			var sum=0.0;
			Ext.each(records,function(record){
				sum+=record.get('grandCost');
			});
			summaryText.setText('摘要：总价：'+Ext.util.Format.number(sum,'0,000.00'));
		}
	});
    var tree = Ext.create('Ext.tree.Panel', {
        title: '项目材料分析',
        width: 960,
        height: 600,
        renderTo: 'project-material-analyze-container',
        useArrows: true,
        rootVisible: true,
        store: modelStore,
        singleExpand: false,
		dockedItems : [{
			xtype : 'toolbar',
			dock : 'top',
			items : ['项目：', projectCombo, '物品关键词：', keywordInput, searchButton ]
		},{
			xtype: 'toolbar',
			dock : 'bottom',
			items: [summaryText]
		}],
        columns: [{
            xtype: 'treecolumn',
            text: '物品',
            flex: 4,
            sortable: false,
            dataIndex: 'name'
        },{
            text: '单位',
            flex: 1,
			align: 'center',
            dataIndex: 'unit',
            sortable: false
        },{
        	text: '使用量',
			flex:3,
			columns:[{
	            text: '总申购量',
	            flex: 1,
				align: 'right',
	            dataIndex: 'totalRequisition',
	            sortable: false,
				renderer: renderFn
	        },{
	            text: '总入库量',
	            flex: 1,
				align: 'right',
	            dataIndex: 'totalWarehouse',
	            sortable: false,
				renderer: renderFn
	        },{
	            text: '总报销量',
	            flex: 1,
				align: 'right',
	            dataIndex: 'totalReceipted',
	            sortable: false,
				renderer: renderFn
	        },]
        },{
			text: '单价',
			flex:3,
			columns:[{
	            text: '最高单价',
	            flex: 1,
				align: 'right',
	            dataIndex: 'maxUnitPrice',
	            sortable: false,
				renderer: renderFn
	        },{
	            text: '平均单价',
	            flex: 1,
				align: 'right',
	            dataIndex: 'averageUnitPrice',
	            sortable: false,
				renderer: renderFn
	        },{
	            text: '最低单价',
	            flex: 1,
				align: 'right',
	            dataIndex: 'minUnitPrice',
	            sortable: false,
				renderer: renderFn
	        }]
		},{
            text: '平均价格',
            flex: 2,
			align: 'right',
            dataIndex: 'averagePrice',
            sortable: false,
			renderer: renderFn
        },{
            text: '总花费',
            flex: 2,
			align: 'right',
            dataIndex: 'grandCost',
            sortable: false,
			renderer: renderFn
        },{
            xtype:'actioncolumn',
            width:32,
            items: [{
                getClass : function(value, meta, record){
                    var tokenId=record.get('id');
					if(tokenId.indexOf("o_") >= 0){
						return 'standalone-icon icon-magnifier';
					}
					return '';
				},
                tooltip: '查看详情',
                handler: function(grid, rowIndex, colIndex) {
                    var rec = grid.getStore().getAt(rowIndex);
		
					var url=$viewMaterialUrl.replace('__projectId__', projectCombo.getValue())
                			.replace('__materialModelId__',rec.get('id').replace('o_',''));
                    window.open(url);
                }
            }]
        }]
    });
});
EOF;

Yii::app()->clientScript->registerScript('dynamic-project-material-analyze', $js, CClientScript::POS_HEAD);

?>